/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MyComRobot.java
 *
 * Created on 8 déc. 2011, 09:59:07
 */
package my.monitor;

import java.beans.PropertyChangeEvent;
import my.monitor.controller.MyController;
import my.monitor.model.enumeration.CommunicationStatus;
import my.monitor.model.enumeration.MyLevel;
import my.monitor.model.enumeration.RobotStatus;

/**
 *
 * @author Piro
 */
public class MyComRobotPanel extends AbstractViewPanel {

    private MyController controller;
    RobotStatus status;

    public MyComRobotPanel() {
        initComponents();
    }
    
    /** Creates new form MyComRobot */
    public MyComRobotPanel(MyController controller) {
        initComponents();
        this.controller = controller;
        //this.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonConnectRobot = new javax.swing.JButton();
        jTextFieldRobotStatus = new javax.swing.JTextField();
        jTextFieldBatteryLevel = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jTextVersion = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(410, 100));
        setMinimumSize(new java.awt.Dimension(410, 100));
        setPreferredSize(new java.awt.Dimension(410, 100));

        jButtonConnectRobot.setText("Connect robot");
        jButtonConnectRobot.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonConnectRobotMouseClicked(evt);
            }
        });

        jTextFieldRobotStatus.setText("Not connected");

        jLabel1.setText("Battery level");

        jLabel2.setText("Version");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(29, 29, 29)
                        .add(jButtonConnectRobot))
                    .add(layout.createSequentialGroup()
                        .add(53, 53, 53)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(jLabel1))))
                .add(30, 30, 30)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jTextVersion)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jTextFieldBatteryLevel)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jTextFieldRobotStatus, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE))
                .addContainerGap(103, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButtonConnectRobot)
                    .add(jTextFieldRobotStatus, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTextFieldBatteryLevel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                    .add(jLabel2)
                    .add(jTextVersion, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(24, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jButtonConnectRobotMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonConnectRobotMouseClicked

    if (status == RobotStatus.CONNECTED) {
        controller.changeRobotStatus(RobotStatus.DISCONNECTED);
    } else {
        controller.changeRobotStatus(RobotStatus.WAIT_CONNEXION);
    }
}//GEN-LAST:event_jButtonConnectRobotMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonConnectRobot;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextFieldBatteryLevel;
    private javax.swing.JTextField jTextFieldRobotStatus;
    private javax.swing.JTextField jTextVersion;
    // End of variables declaration//GEN-END:variables

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(
                MyController.MONITOR_BATTERY_PROPERTY)) {
            MyLevel level = (MyLevel) evt.getNewValue();
            jTextFieldBatteryLevel.setText(level.name());
        } else if (evt.getPropertyName().equals(
                MyController.CHANGE_ROBOT_STATUS)) {
            status = (RobotStatus) evt.getNewValue();
            jTextFieldRobotStatus.setText(status.name());
            System.out.println("view robot status :  " + status.name());
            if (status == RobotStatus.WAIT_CONNEXION) {
                jButtonConnectRobot.setEnabled(false);
            } else if (status == RobotStatus.CONNECTED) {
                jButtonConnectRobot.setEnabled(false);
                jButtonConnectRobot.setText("Disconnect robot");
            } else {
                jButtonConnectRobot.setEnabled(true);
                jButtonConnectRobot.setText("Connect robot");
            }
        } else if (evt.getPropertyName().equals(
                MyController.COMMUNICATION_STATUS_PROPERTY)) {
            CommunicationStatus commStatus = (CommunicationStatus) evt.getNewValue();
            if (commStatus != CommunicationStatus.COMMUNICATION_CONNECTED) {
                this.setVisible(false);
            } else {
                this.setVisible(true);
            }
        }  else if (evt.getPropertyName().equals(
                MyController.VERSION)) {
            jTextVersion.setText((String)evt.getNewValue());
        }
    }
}
